<?php
function seed()
{
list($msec, $sec) = explode(' ', microtime());
return (float) $sec;
}



echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml">';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
echo '<meta http-equiv="Pragma" content="no-cache"/>';
echo  '<title>吃哪家？ - 游味</title>';
echo '</head>';

echo '<body>';

$f = fopen("data.txt", "r");
$item_str = fgets($f, 4096);
$name_str = fgets($f, 4096);
//echo $item_str;
//echo $name_str;
fclose($f);


//$items = array(3,5,7);
//$names = array("sA", "sB", "sC");

$items = split(" ", $item_str);
$names = split(" ", $name_str);

$sum = 0;
$result_idx = 0;
for ($i = 0; $i < count($items); $i++)
{
	$sum += $items[$i];
}

while(1)
{
	srand(seed());	
	$x = rand(0, $sum);
	$sum2 = 0;
	for ($i = 0; $i < count($items); $i++)
	{
		$sum2 += $items[$i];
		if ($x < $sum2)
		{
			break;
		}
	}
	if ($i < count($items))
	{
		$result_idx = $i;
		break;
	}
}



echo "<h2>随机结果</h2>";
echo "<p>" . $items[$result_idx] . " " . $names[$result_idx] . "</p>";


echo '<form method="POST" action="mark.php" style="display:inline"><input type="submit" name="add" value="加分"/><input type="hidden" name="val" value="add" /><input type="hidden" name="id" value="' . $result_idx . '" /></form>';
echo '<form method="POST" action="mark.php" style="display:inline"><input type="submit" name="add" value="扣分"/><input type="hidden" name="val" value="dec" /><input type="hidden" name="id" value="' . $result_idx . '" /></form>';

echo "<hr />";

echo "<h2>列表</h2>";
for ($i = 0; $i < count($items); $i++)
{
	echo $items[$i] . "  "  . $names[$i] . "<br />";
}


// footer
echo '<br /><hr />';
echo '<div id="footer">';
echo '游味作品. 版权所有 @2010';
echo '</div>';

echo '</body>';
echo '</html>';

?>

